<template>
    <div class="box">
        <header class="header">
            <van-nav-bar
                title="提交订单"
                left-text="返回"
                left-arrow
                @click-left="$router.push('/cart')"
                />
        </header>
        <div class="content">
            <div class="address">
                <van-contact-card v-if="addressExist"
                    type="edit"
                    :name="currentContact.name"
                    :tel="currentContact.tel"
                    @click="onEdit"
                />
                <!-- <van-notice-bar v-else mode="link">是否添加一个地址</van-notice-bar> -->
                <router-link v-else 
                :to="{ name: 'order2', params: { time: time }}"
                tag="van-notice-bar"
                mode="link"
                >
                是否添加一个地址</router-link>
            </div>
            <div class="card">
                <van-card v-for="(item, index) in cartList" :key="index"
                    :num="item.num"
                    :price="item.originprice"
                    :title="item.proname"
                    :thumb="item.img1"
                />
            </div>
            <div>
                <van-submit-bar :price="totalPrice" button-text="提交订单" @submit="onSubmit" />
            </div>
        </div>
    </div>
</template>

<script>
import router from '../../router'
import Vue from 'vue'
import { SubmitBar, NoticeBar, Toast, ContactCard, AddressList, NavBar, Card } from 'vant'
import { addressList, update, defaultAddress, addOrder, confirmOrder } from '../../api/order'

Vue.use(SubmitBar)
Vue.use(NoticeBar)
Vue.use(Toast)
Vue.use(ContactCard)
Vue.use(AddressList)
Vue.use(NavBar)
Vue.use(Card)
export default {
  data () {
    return {
      length: 0,
      totalPrice: 0,
      gx: '',
      currentContact: {
        name: '张三',
        tel: '13000000000'
      },
      time: '',
      addressExist: false,
      cartList: []
    }
  },
  async mounted () {

    // console.log(this.$route.params.addressid);
    // if(this.$route.params.addressid === ''){
    //   console.log(1);
    // } else {
    //   console.log(2);
    // }

    // this.currentContact.name = this.$route.params.selectAddressMessage.name
    // this.currentContact.tel = this.$route.params.selectAddressMessage.tel
    // console.log(this.$router.params.selectAddressMessage);

    // 1、提交订单信息后获取的时间戳
    const res = await addOrder({ userid: localStorage.getItem('userid') })
    // code: '200', message: '添加订单', time: '1631341485874'
    this.time = res.data.time
    console.log('提交订单 ' + this.time)

    // 2、在订单页获取订单数据
    confirmOrder({ userid: localStorage.getItem('userid'), time: this.time }).then(res => {
      this.cartList = res.data.data
      res.data.data.forEach(item => {
        this.totalPrice += item.originprice
        // console.log(item.originprice);
      })
    })



    // 3、查询默认地址
    const res1 = await defaultAddress({ userid: localStorage.getItem('userid') })
    // code: '200', message: '查询默认地址', data: Array(0) 是默认地址
    // console.log(res1)
    // console.log(res1.data.data[0]);

    // 数组长度为0，没有默认地址地址，去找地址列表的最后一个
      if (res1.data.data.length === 0) {
        // this.addressExist = false
        addressList({ userid: localStorage.getItem('userid') }).then(res => {
          // 地址列表有地址
          if (res.data.data.length > 0) {
            this.addressExist = true  // 地址列表有，传入最后一个
            this.length = res.data.data.length-1
            this.currentContact.name = res.data.data[this.length].name
            this.currentContact.tel = res.data.data[this.length].tel
          // 地址列表没地址 显示添加地址
          } else {  
            this.addressExist = false
          }
        })
      // 有默认数组
      } else {
        this.addressExist = true
        this.currentContact.name = res1.data.data[0].name
        this.currentContact.tel = res1.data.data[0].tel
      }
  },
  methods: {
    //  有地址,点击修改订单地址
    onEdit () {
      //  跳转地址列表
      // this.$router.push('/order2')
      router.push({ name:'order2', params: { time: this.time}})
    },
    onSubmit () {

    }
  }
}
</script>
